package com.xiangmo.travel.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiangmo.travel.Annotation.TokenRequired;
import com.xiangmo.travel.Dto.NoticeConfirmDto;
import com.xiangmo.travel.Dto.NoticeDto;
import com.xiangmo.travel.Dto.ResultData;
import com.xiangmo.travel.Entity.Notice;
import com.xiangmo.travel.Enums.CodeAndMsg;
import com.xiangmo.travel.Service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;
    //生成一个通知
    @TokenRequired
    @PostMapping("/creat")
    public ResultData<Integer> createNotice(@RequestBody NoticeDto noticeDto, @RequestHeader("Authorization") String token){
        int status = noticeService.createNotice(noticeDto);
        if(status >= 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("生成通知失败！状态码：[{}]",status);
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }
    //通过用户ID查询通知
    @TokenRequired
    @GetMapping("/get/{userId}")
    public ResultData<Page<Notice>> getNoticeByUserId(@PathVariable Long userId, @RequestParam int pageNum,
                                                 @RequestParam int pageSize, @RequestHeader("Authorization") String token){
        Page<Notice> page = noticeService.getNoticeByUserId(userId, pageNum, pageSize);
        return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                page, token.substring(7));
    }


    //修改通知的状态为已读
    @TokenRequired
    @PutMapping("/update/{id}")
    public ResultData<Integer> updateNoticeStatus(@PathVariable Long id, @RequestHeader("Authorization") String token){
        int status = noticeService.updateNoticeStatus(id);
        if(status == 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("修改通知的状态为已读失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }
    //这里后期需要校验数据库是否已经存在才能插入 ===================
    //确认或拒绝成为组员的申请
    @TokenRequired
    @PostMapping("/confirm")
    public ResultData<Integer> updateNoticeConfirm(@RequestBody NoticeConfirmDto noticeConfirmDt, @RequestHeader("Authorization") String token){
        int status = noticeService.updateNoticeConfirm(noticeConfirmDt);
        if(status >= 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    status, token.substring(7));
        }
        log.warn("确认或拒绝成为组员的申请失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                status, token.substring(7));
    }

    //管理员审核景点
    @GetMapping("/confirm/{noticeId}/{attractionsId}/{userId}")
    @TokenRequired
    public ResultData<Integer> updateAttractionsStatus(@PathVariable Long noticeId, @PathVariable Long attractionsId, @PathVariable Long userId,
                                                       @RequestParam int status, @RequestHeader("Authorization") String token){
        int ok = noticeService.isPublishedAttractions(noticeId, attractionsId, userId, status);
        if(ok >= 1){
            return new ResultData<>(CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMessage(),
                    null, token.substring(7));
        }
        log.warn("管理员审核景点失败！");
        return new ResultData<>(CodeAndMsg.ERROR.getCode(), CodeAndMsg.ERROR.getMessage(),
                null, token.substring(7));
    }

}
